package asia.lxq.cic.controller;

import asia.lxq.cic.config.GlobalConfig;
import asia.lxq.cic.dao.AreaMapper;
import asia.lxq.cic.dao.GroupMapper;
import asia.lxq.cic.dao.UserMapper;
import asia.lxq.cic.entity.Result;
import asia.lxq.cic.entity.User;
import asia.lxq.cic.service.UserService;
import asia.lxq.cic.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController()
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserMapper userMapper;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    UserService userService;
    @Autowired
    AreaMapper areaMapper;
    @Autowired
    GroupMapper groupMapper;

    @GetMapping("/get_login_info")
    public Result<User> getLoginUserInfo(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user= (User) redisUtil.get(GlobalConfig.PRE_USERINFO_REDIS+authentication.getPrincipal());
//        user.setGroup(groupMapper.selectById(user.getGroupId()));
//        user.query(areaMapper);
//        System.out.println(user);
        return Result.success(user);
    }

    @PostMapping("/update_user_base")
    public Result<Object> updateUserBaseInfo(@RequestBody User user){
        return userService.updateUserBaseInfoWithAop(user);
    }

    @PostMapping("/update_user_pic")
    public Result<Object> updateUserPic(@RequestPart("resourceFile")MultipartFile resourceFile,Authentication authentication){
        return userService.updateUserPic(resourceFile,authentication);
    }
}
